package org.darkforest.admin.controller;

import org.darkforest.common.services.*;
import org.darkforest.common.util.JSONUtils;
import org.darkforest.common.util.Page;
import org.darkforest.dao.mapper.ActiveAreaGoodsMapper;
import org.darkforest.dao.mapper.CartMapper;
import org.darkforest.dao.mapper.GoodsCommonMapper;
import org.darkforest.dao.mapper.GoodsMapper;
import org.darkforest.dao.mapper.StoreMapper;
import org.darkforest.model.*;
import org.darkforest.ucenter.auth.AuthPassport;
import org.darkforest.vo.StoreClassVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by admin on 2015/9/6.
 */
@Controller
@RequestMapping("store")
public class StoreController {
    @Autowired
    private StoreJoinInService storeJoinInService;
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private StoreGradeService storeGradeService;
    @Autowired
    private StoreClassService storeClassService;
    @Autowired
    private StoreService storeService;
    @Autowired
    private ActiveAreaGoodsMapper activeAreaGoodsMapper;
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsCommonMapper goodsCommonMapper;

    @AuthPassport
    @RequestMapping("storeList")
    public ModelAndView storeList(Integer page, Integer gradeId,String memberName,String storeName, Integer scId, HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("store/store_list");
        Store store = new Store();
        store.setStart(page == null ? 1 : page);
        store.setPageSize(Page.DEFAULT_PAGE_SIZE);
        store.setStoreName(storeName == null ? "" : storeName);
        store.setMemberName(memberName == null ? "" : memberName);
        store.setScId(scId);
        store.setGradeId(gradeId);
        List<Store>  stores= storeMapper.findStoreByCondition(store);
        store.setResultList(stores);
        modelAndView.addObject("modelPage", store);
        return modelAndView;
    }

    @AuthPassport
    @RequestMapping("storeList/storeJoininList")
    public ModelAndView storeJoininList(Integer page, String storeName,String memberName,String joininState, Integer sgId, HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("store/storeJoinin_list");
        StoreJoinIn storeJoinin = new StoreJoinIn();
        storeJoinin.setStart(page == null ? 1 : page);
        storeJoinin.setPageSize(Page.DEFAULT_PAGE_SIZE);
        storeJoinin.setStoreName(storeName == null ? "" : storeName);
        storeJoinin.setMemberName(memberName == null ? "" : memberName);
        storeJoinin.setJoininState(joininState == null ? "" : joininState);
        storeJoinin.setSgId(sgId);
        List<StoreJoinIn> storeJoinInList=storeJoinInService.findStoreJoinInByCondition(storeJoinin);
        storeJoinin.setResultList(storeJoinInList);
        modelAndView.addObject("modelPage", storeJoinin);
        return modelAndView;
    }

    @AuthPassport
    @RequestMapping("storeList/toCheck")
    public ModelAndView toCheck(Integer memberId){
        ModelAndView modelAndView = new ModelAndView("store/storeJoinin_check");
        StoreJoinIn storeJoinin = storeJoinInService.selectByPrimaryKey(memberId);
        StoreClass storeClass = storeClassService.selectByPrimaryKey(storeJoinin.getScId());
        StoreGrade storeGrade = storeGradeService.selectByPrimaryKey(storeJoinin.getSgId());
        BigDecimal total = new BigDecimal("0.00");
        total = storeGrade.getSgPrice().add(new BigDecimal(storeClass.getScBail()));
        storeJoinin.setPayingAmount(total);
        modelAndView.addObject("storeJoinin",storeJoinin);
        modelAndView.addObject("storeClass",storeClass);
        modelAndView.addObject("storeGrade",storeGrade);
        if(storeJoinin.getStoreClassIds()!= null){
            List<StoreClassVo> list= JSONUtils.toList(storeJoinin.getStoreClassIds(), StoreClassVo.class);
            modelAndView.addObject("storeClassList", list);
        }
        return modelAndView;
    }
    @AuthPassport
    @RequestMapping("doCheck")
    public String  doCheck(StoreJoinIn storeJoinin){
       // ModelAndView modelAndView = new ModelAndView("store/storeJoinin_list");
        storeJoinInService.updateByPrimaryKey(storeJoinin);
       // modelAndView.addObject("storeJoinin",storeJoinin);
        return  "redirect:/store/storeJoininList";
    }
    @AuthPassport
    @RequestMapping("storeList/toView")
    public ModelAndView toView(Integer memberId){
        ModelAndView modelAndView = new ModelAndView("store/storeJoinin_view");
        StoreJoinIn storeJoinin = storeJoinInService.selectByPrimaryKey(memberId);
        StoreClass storeClass = storeClassService.selectByPrimaryKey(storeJoinin.getScId());
        StoreGrade storeGrade = storeGradeService.selectByPrimaryKey(storeJoinin.getSgId());
        modelAndView.addObject("storeJoinin",storeJoinin);
        modelAndView.addObject("storeClass",storeClass);
        modelAndView.addObject("storeGrade",storeGrade);
        if(storeJoinin.getStoreClassIds()!= null){
            List<StoreClassVo> list= JSONUtils.toList(storeJoinin.getStoreClassIds(), StoreClassVo.class);
            modelAndView.addObject("storeClassList", list);
        }
        return modelAndView;
    }
    @AuthPassport
    @RequestMapping("storeList/toCheckAgain")
    public ModelAndView toCheckAgain(Integer memberId){
        ModelAndView modelAndView = new ModelAndView("store/storeJoinin_checkAgain");
        StoreJoinIn storeJoinin = storeJoinInService.selectByPrimaryKey(memberId);
        StoreClass storeClass = storeClassService.selectByPrimaryKey(storeJoinin.getScId());
        StoreGrade storeGrade = storeGradeService.selectByPrimaryKey(storeJoinin.getSgId());
        modelAndView.addObject("storeJoinin",storeJoinin);
        modelAndView.addObject("storeClass",storeClass);
        modelAndView.addObject("storeGrade",storeGrade);
        if(storeJoinin.getStoreClassIds()!= null){
            List<StoreClassVo> list= JSONUtils.toList(storeJoinin.getStoreClassIds(), StoreClassVo.class);
            modelAndView.addObject("storeClassList", list);
        }
        return modelAndView;
    }
    @AuthPassport
    @RequestMapping("doCheckAgain")
    public String doCheckAgain(StoreJoinIn storeJoinin){
        storeService.addStore(storeJoinin);
        return  "redirect:/store/storeJoininList";
    }

    @AuthPassport
    @RequestMapping("storeList/toChangeStore")
    public ModelAndView toChangeStore(Integer storeId){
        ModelAndView modelAndView = new ModelAndView("store/store_change");
        Store store = storeMapper.selectStoreByPrimaryKey(storeId);
        modelAndView.addObject("store", store);
        return modelAndView;
    }
    @AuthPassport
    @RequestMapping("doChangeStore")
    public String doChangeStore(@ModelAttribute Store store){

        //ModelAndView modelAndView = new ModelAndView("store/store_list");
        storeMapper.updateStoreByPrimaryKey(store);

        /**********修改申请信息**************/
        StoreJoinIn storeJoinin = new StoreJoinIn();
        storeJoinin.setMemberId(store.getMemberId());
        storeJoinin.setStoreName(store.getStoreName());
        storeJoinInService.updateStoreJoinInByPrimaryKey(storeJoinin);

        /**********关闭店铺操作**************/
        if(store.getStoreState()!=null&&store.getStoreState() == 0){
            storeMapper.closeStore(store);
        }

        // 店铺名称修改时的关联修改
        /**********修改专区**************/
        ActiveAreaGoods activeAreaGoods = new ActiveAreaGoods();
        activeAreaGoods.setStoreId(store.getStoreId());
        activeAreaGoods.setStoreName(store.getStoreName());
        activeAreaGoodsMapper.updateStoreName(activeAreaGoods);

        /**********修改购物车**************/
        Cart cart = new Cart();
        cart.setStoreId(store.getStoreId());
        cart.setStoreName(store.getStoreName());
        cartMapper.updateStoreName(cart);

        /**********修改商品**************/
        Goods goods = new Goods();
        goods.setStoreId(store.getStoreId());
        goods.setStoreName(store.getStoreName());
        goodsMapper.updateStoreName(goods);

        /**********修改商品公共信息**************/
        GoodsCommon goodsCommon = new GoodsCommon();
        goodsCommon.setStoreId(store.getStoreId());
        goodsCommon.setStoreName(store.getStoreName());
        goodsCommonMapper.updateStoreName(goodsCommon);

        //return modelAndView;
        return "redirect:/store/storeList";
    }
}